import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:FiduMart/app_routes.dart';
import 'package:FiduMart/constants/color_constant.dart';
import 'package:FiduMart/core/db/sqlite.dart';
import 'package:FiduMart/core/network/RequestClient.dart';

import 'package:FiduMart/core/utils/logger.dart';
import 'package:FiduMart/core/utils/pref_utils.dart';
import 'package:FiduMart/firebase_options.dart';
import 'package:FiduMart/service/setting.dart';

import 'localization/app_localization.dart';



void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  Logger.init(LogMode.debug);
  // 集成firebase
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  final pre = PrefUtils();
  await pre.init();
  Get.put(pre);
  var token = await pre.getToken();
  if (token.isNotEmpty) {
    RequestClient().setToken(token);
  }
  Get.put(Setting());
  Locale? locale = await Setting.getLocale();
  //todo 多语言
  // Locale? locale = Locale('zh', 'HK');
  // Locale? locale = Locale('en', 'US');
  final db = SqliteDB();
  await db.init();
  Get.put(db);
  runApp(MyApp(locale: locale));

}

class MyApp extends StatelessWidget {
  MyApp({required this.locale});
  Locale? locale;
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'UTON Exchange',
      theme: ThemeData(
        colorScheme: ColorScheme.dark(
          primary: Colors.black, // 主要颜色
          // secondary: Colors.green, // 次要颜色
          surface: Colors.blue[400]!, // 表面颜色(如按钮背景)
          // error: Colors.red, // 错误颜色
          background: Colors.white, // 背景颜色
          onPrimary: ColorConstant.colorFFF3F4F8, // 用于主要颜色上的文字和图标颜色
          // onSecondary: Colors.white, // 用于次要颜色上的文字和图标颜色
          // onSurface: Colors.white, // 用于表面颜色上的文字和图标颜色
          // onBackground: Colors.white, // 用于背景颜色上的文字和图标颜色
          // onError: Colors.white, // 用于错误颜色上的文字和图标颜色
          // brightness: Brightness.light, // 亮色主题
        ),
        useMaterial3: true,
        splashColor: Colors.transparent,
      ),
      // initialBinding: InitialBindings(),
      initialRoute: AppRoutes.initialRoute,
      getPages: AppRoutes.pages,
      builder: EasyLoading.init(),
      translations: AppLocalization(),
      defaultTransition: Transition.rightToLeft,
      locale: locale, //for setting localization strings
      // fallbackLocale: Locale('en', 'US'),
    );
  }
}



